/**
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 *
 *MpmProjectTeamController
 * 项目任命-项目组成员
 *
 * 修改纪录
 * 2020-12-28 版本：1.0 lanjianwei 创建。
 * @version 版本：1.0
 * @author 作者：lanjianwei
 * 创建日期：2020-12-28
 */

package com.topscomm.mpm.controller;

import com.topscomm.basic.exception.MessageException;
import com.topscomm.mpm.MpmSystemConst;
import com.topscomm.mpm.pojo.MpmProjectAppointmentEntity;
import com.topscomm.mpm.pojo.MpmProjectTaskEntity;
import com.topscomm.mpm.pojo.MpmProjectTeamEntity;
import com.topscomm.mpm.service.impl.MpmProjectAppointmentService;
import com.topscomm.mpm.service.impl.MpmProjectTaskService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.topscomm.mpm.controller.auto.MpmProjectTeamControllerAuto;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: lanjianwei
 * @date: 2020-12-28
 * @description:项目任命-项目组成员Controller
 * @modify:
 */
@RestController
@RequestMapping("mpm/projectTeam")
public class MpmProjectTeamController extends MpmProjectTeamControllerAuto implements IMpmProjectTeamController {

	@Autowired
	private MpmProjectTaskService mpmProjectTaskService;
	@Autowired
	private MpmProjectAppointmentService mpmProjectAppointmentService;

	/**
	 * @author: lanjianwei
	 * @date: 2020-12-28
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("parentid")) {
			String value = paraMap.get("parentid").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and appointmentid = '{0}' ", value);
		}
		if (paraMap.containsKey("teamType")) {
			String value = paraMap.get("teamType").toString();
			if (!StringUtil.isEmpty(value) && !"0".equals(value))
				sql.appendSingle(" and teamid = {0} ", value);
		}
		if (paraMap.containsKey("qry_user")) {
			String value = paraMap.get("qry_user").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (usercode = '{0}' or username like '%{0}%') ", value);
		}
		if (paraMap.containsKey("qry_projectcode")) {
			String value = paraMap.get("qry_projectcode").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and projectcode = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_appointmenttype")) {
			String value = paraMap.get("qry_appointmenttype").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and appointmenttype in ({0}) ", value);
		}
		if (paraMap.containsKey("qry_teamType")) {
			String value = paraMap.get("qry_teamType").toString();
			if (!StringUtil.isEmpty(value) && !"0".equals(value))
				sql.appendSingle(" and teamid = {0} ", value);
		}
		if (paraMap.containsKey("qry_state")) {
			String value = paraMap.get("qry_state").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and state = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_enabled")) {
			String value = paraMap.get("qry_enabled").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and enabled = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_candelete")) {
			String value = paraMap.get("qry_candelete").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and candelete = '{0}' ", value);
		}
		// 任命-项目组列表展示，通过projectcode获取最新任命id
		if (paraMap.containsKey("qry_projectcodeforappointid")) {
			String value = paraMap.get("qry_projectcodeforappointid").toString();
			if (!StringUtil.isEmpty(value)) {
				StringBufferProxy sqlForAppointid = new StringBufferProxy();
				sqlForAppointid.appendSingle(" projectcode = '{0}' and state = 9 ", value);
				sqlForAppointid.appendSingle(" order by createon desc ");
				// 查询最近上一次生效的任命书
				List<MpmProjectAppointmentEntity> mpmProjectAppointmentEntities = mpmProjectAppointmentService.queryByWhere(sqlForAppointid.toString());
				// 如果存在生效的任命书，则使用任命id查询，否则使用projectcode查询
				if(mpmProjectAppointmentEntities.size() > 0) {
					sql.appendSingle(" and appointmentid = '{0}' ", mpmProjectAppointmentEntities.get(0).getId());
				} else {
					sql.appendSingle(" and projectcode = '{0}' ", value);
				}
			}
		}
		if (paraMap.containsKey("qry_where")) {
			String value = paraMap.get("qry_where").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and {0}", value);
		}
		return sql.toString();
	}

	/**
	 * @author: lanjianwei
	 * @date: 2020-12-28
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	@GetMapping(value = "/queryList")
	public ResponseResult<Object> queryList(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			Query query = new Query(paraMap);
			String sqlWhere = this.spellListSql(paraMap);
			int totalCount = this.service.getCount(sqlWhere);
			List<Map<String, Object>> tempList = null;
			if (query.getPageSize() > 0) {
				tempList = this.service.queryMapForPage(sqlWhere, query.getCurrentPage(), query.getPageSize(),
						query.getSidx(), query.getSord());
			} else {
				if (paraMap.containsKey("sidx") && paraMap.containsKey("sord")) {
					String sidx = paraMap.get("sidx").toString();
					String sord = paraMap.get("sord").toString();
					if (!StringUtil.isEmpty(sidx) && !StringUtil.isEmpty(sord))
						sqlWhere += " order by projectrole asc, " + sidx + " " + sord + ", id asc";
				}
				tempList = this.service.queryMapByWhere(sqlWhere);
			}
			Page page = new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
			return ResponseResult.ok(page);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}
	/**
	 * @author: houyuhang
	 * @date: 2022-05-16
	 * @param: paraMap
	 * @description: 新增撤销项目组成员执行方法
	 * @modify:
	 **/
	@PostMapping(value = "/insertRevoke")
	public ResponseResult<Object> insertRevoke(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			this.service.insertRevoke(paraMap);
			return ResponseResult.ok("撤销成功");
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}
	/**
	 * @author: houyuhang
	 * @date: 2022-05-16
	 * @param: paraMap
	 * @description: 删除撤销项目组成员执行方法
	 * @modify:
	 **/
	@DeleteMapping(value = "/deleteRevoke")
	public ResponseResult<Object> deleteRevoke(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			this.service.deleteRevoke(paraMap);
			return ResponseResult.ok("取消撤销成功");
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}
	/**
	 * @author: houyuhang
	 * @date: 2022-07-15
	 * @param: paraMap
	 * @description: 重写datacodelist
	 * @modify:
	 **/
	@Override
	@GetMapping(value = "/dataCodeList")
	public ResponseResult<Object> dataCodeList(@RequestParam(required = false) Map<String, Object> paraMap) {
		ResponseResult<Object> res = super.dataCodeList(paraMap);
		Map<String, Object> codeMap = (Map<String, Object>) res.getResult();
		List<Map<String, Object>> businessRoleDutyList = datacodeCache.getCodeListMap("MpmBusinessRoleDuty", "", "sortcode");
		codeMap.put("businessRoleDutyList", businessRoleDutyList);
		List<Map<String, Object>> bidGuideDutyList = datacodeCache.getCodeListMap("MpmBidGuideDuty", "", "sortcode");
		codeMap.put("bidGuideDutyList", bidGuideDutyList);
		List<Map<String, Object>> bidBookDutyList = datacodeCache.getCodeListMap("MpmBidBookDuty", "", "sortcode");
		codeMap.put("bidBookDutyList", bidBookDutyList);
		return ResponseResult.ok(codeMap);
	}
}

